package com.chenshu.shop.service;

import com.chenshu.shop.mapper.OrderMapper;
import com.chenshu.shop.model.Cart;
import com.chenshu.shop.model.Item;
import com.chenshu.shop.model.Order;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created with IntelliJ IDEA
 * Description
 * User：chenshu
 * Date：2024-06-07
 * Time：10:38
 */
@Service
@Slf4j
public class OrderService {
    @Autowired
    OrderMapper orderMapper;

    @Transactional
    public Integer addOrder(Integer userId) {
        Order order = new Order();
        order.setUserId(userId);
        //新增一条订单信息
        Integer row1 = orderMapper.insertOrder(order);
        if (row1 == 1) {
            log.info("插入订单成功！");
        } else {
            log.info("插入订单失败！");
        }
        Integer orderId = order.getId();
        //用于计算总价
        int sum = 0;
        //获取购物车列表
        List<Cart> cartList = orderMapper.getCartsByUserId(userId);
        for (Cart cart: cartList) {
            //构造item
            Item item = new Item();
            item.setPrice(cart.getGood().getPrice());
            item.setAmount(cart.getAmount());
            item.setOrderId(orderId);
            item.setGoodId(cart.getGood().getId());
            item.setOrderId(orderId);
            //插入item
            Integer row2 = orderMapper.insertItems(item);
            if (row2 == 1) {
                log.info("订单项目插入成功");
            } else {
                log.info("订单项目插入失败");
            }
            sum += item.getAmount() * item.getPrice();
        }
        //更新订单总价
        Integer row3 = orderMapper.updateOrderTotal(sum, orderId);
        if (row3 == 1) {
            log.info("修改订单总价成功！");
        } else {
            log.info("修改订单总价失败！");
        }
        //清空购物车
        Integer row4 = orderMapper.clearCart(userId);
        if (row4 != cartList.size()) {
            log.info("清空购物车失败！");
        } else {
            log.info("清空购物车成功！");
        }
        return orderId;
    }

    public List<Order> getAllOrders(Integer status, Integer pageIndex, Integer pageSize) {
        Integer begin = (pageIndex-1) * pageSize + 1;
        Integer end = pageIndex * pageSize;
        List<Order> orderList = orderMapper.getAllOrders(status, begin, end);
        for(Order order : orderList) {
            order.setItemList(orderMapper.getItemsByOrderId(order.getId()));
        }
        return orderList;
    }

    public List<Order> getAllOrdersById(Integer userId, Integer pageIndex, Integer pageSize) {
        Integer begin = (pageIndex-1) * pageSize + 1;
        Integer end = pageIndex * pageSize;
        List<Order> orderList = orderMapper.getAllOrdersById(userId, begin, end);
        for(Order order : orderList) {
            order.setItemList(orderMapper.getItemsByOrderId(order.getId()));
        }
        return orderList;
    }


    public Order getOrderById(Integer id) {
        Order order = orderMapper.getOrderById(id);
        order.setItemList(orderMapper.getItemsByOrderId(id));
        return order;
    }



    public Integer getOrderCount(Integer status) {
        return orderMapper.getOrderCount(status);
    }

    public void updateOrderStatus(Integer id, Integer status) {
        Integer row = orderMapper.updateOrderStatus(id, status);
        if (row == 1) {
            log.info("修改订单状态成功");
        } else {
            log.info("修改订单状态失败");
        }
    }

    public Integer getUpdatePage(Integer orderId, Integer status) {
        //获取到刚刚更改过状态的商品所在行数
        Integer row = orderMapper.getOrderRow(orderId, status);
        double updatePage = (double) row /3;
        //进1
        return (int) Math.ceil(updatePage);
    }

    public void deleteOrder(Integer id) {
        Integer row = orderMapper.deleteOrder(id);
        if (row == 1) {
            log.info("删除订单成功");
        } else {
            log.info("删除订单失败");
        }
    }

    public Integer getCountsByUserId(Integer userId) {
        return orderMapper.getCountByUserId(userId);
    }

    public void updateOrder(Order order) {
        Integer row = orderMapper.updateOrderInfo(order);
        if (row == 1) {
            log.info("更新订单信息成功！");
        } else {
            log.info("更新订单信息失败！");
        }
    }
}
